package com.walking.repository;


import com.walking.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Map;
import java.util.Optional;
/**
 * JPA
 * @author walking
 * 公众号：编程大道
 */
public interface UserRepository extends JpaRepository<User, Integer> {

    /**
     * 自定义方法
     * @param name
     * @param age
     * @return
     */
    User findByUserNameAndUserAge(String name, int age);

    /**
     * 自定义原生SQL 返回实体
     * @param name
     * @param age
     * @return
     */
    @Query(value = "select * from t_user where user_name = ?1 and user_age = ?2", nativeQuery = true)
    Optional<User> findByNameAndAgeBySql(String name, int age);

    /**
     * 自定义原生SQL 返回int值
     * @param name
     * @param age
     * @return
     */
    @Query(value = "select user_sex from t_user where user_name = ?1 and user_age = ?2", nativeQuery = true)
    int findSexBySql(String name, int age);


    /**
     * 自定义原生SQL 返回map
     * @param name
     * @param age
     * @return
     */
    @Query(value = "select * from t_user where user_name = ?1 and user_age = ?2", nativeQuery = true)
    Map findBySqlToMap(String name, int age);
}

